package com.sxhuayuan.parking.utils;

import org.springframework.util.Assert;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * csv格式报表导出
 * 
 * @author 何龙
 * @create 2019-08-24 9:01
 */
public class CSVUtil {

	/** 直接返回拼接字符串 */
	public static void sendcsv(String content, String fileName, HttpServletResponse response) throws IOException {
		Assert.notNull(content, "不能为空");
		Assert.notNull(fileName, "不能为空");
		Assert.notNull(response, "不能为空");
		fileName = URLEncoder.encode(fileName + ".csv", "UTF-8");
		response.reset();
		response.setContentType("text/csv;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-disposition", "attachment; filename=" + fileName);
		OutputStream outputStream = response.getOutputStream();
		outputStream.write(content.getBytes("GBK"));
		outputStream.flush();
	}
}
